package com.bbs.web;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.bbs.entity.Article;
import com.bbs.entity.Reply;
import com.bbs.entity.User;
import com.bbs.service.ArticleService;
import com.bbs.service.MessageService;
import com.bbs.service.ReplyService;
import com.bbs.util.DateUtil;
import com.bbs.util.Page;

public class ReplyServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;

	/**
	 * doGet方法
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String method = request.getParameter("_method");
		if("save".equals(method)) {
			save(request, response);
		} else if("findmine".equals(method)){
			findMine(request,response);
		} else {
			response.sendError(404);
		} 
	}

	/**
	 * 找到某一用户的所有回帖
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	private void findMine(HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		User u = (User) request.getSession().getAttribute("user");
		if(u == null){
			response.sendRedirect("index.jspx?_method=login");
		} else {
			Page page = new Page();
			page.setTotleSize(new ReplyService().getCountByuid(u.getId()));
			if(request.getAttribute("p") != null){
				page.setCurrentPage(Integer.parseInt(request.getParameter("p")));
			}
			List<Reply> rlist = new ReplyService().findMineByUid(u.getId(),page);
			if(rlist == null){
				rlist = new ArrayList<Reply>();
			}
			request.getSession().setAttribute("myreplylist", rlist);
			request.getSession().setAttribute("page", page);
			response.sendRedirect("index.jspx?_method=usercenter");
		}
		
	}

	/**
	 * 回帖保存
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	private void save(HttpServletRequest request, HttpServletResponse response) 
			throws ServletException, IOException {
		String articleid = request.getParameter("articleid");
		String content = request.getParameter("content");
		HttpSession session = request.getSession();
		User user = (User)session.getAttribute("user");
		String pid = request.getParameter("pid");
		int rows = 0;
		if(user != null) {
			rows = new ReplyService().save(articleid, pid, content, user);
		}
		if(rows > 0 ) {
			user.setScore(user.getScore() + 2);
			Article a = new ArticleService().findById(Integer.parseInt(articleid));
			String messagecontent = user.getUsername() + "在" + DateUtil.getNow() + "回复了您的帖子：<a href=\"article.jspx?_method=show&id=" + a.getId() + "\">" + a.getName() + "</a>";
			new MessageService().sendMessage(user,a,messagecontent);
			response.sendRedirect("article.jspx?_method=show&id="+articleid);
		} else {
			response.sendError(404);
		}
	}

	/**
	 * doPost方法
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request, response);
	}

}
